﻿Imports XL.Common
Imports XL.Common.Utils
Imports AssetVisionXAuto.Facade
Imports System.Collections
Imports System.Data

Namespace Facade

    Public Class OpBizManage

        Public Shared Function GetAutoMoveFormCode( _
            ByVal preview As Boolean, _
            ByVal toLocationID As String) As String

            Dim resultBuilder As New LineStrBuilder

            Dim locationRow As AssetVisionXAuto.FTs.FT_M_AV_LOCATIONRow = _
                AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_LOCATIONRow(toLocationID)

            If IsNothing(locationRow) = True Then
                Return String.Empty
            End If

            Dim codePrefix As String = SysInfo.ReadShareSysInfo(Decls.SVN_MOVE_CODE_PREFIX)
            Dim codeDigitLength As Integer = CommTK.FInteger(SysInfo.ReadShareSysInfo(Decls.SVN_FORM_CODE_DIGHT_LENGTH))

            resultBuilder.AppendFormat("{0}{1}", codePrefix, locationRow.LOCATION_CODE)
            If preview = True Then
                resultBuilder.AppendFormat(StrDup(codeDigitLength, "X"))
                Return resultBuilder.ToString
            End If

            Dim seedName As String = String.Format("{0}_{1}", Decls.SPX_MOVE_FORM_CODE, resultBuilder.ToString)
            Dim seedValue As Integer = Facade.OpSysConfig.GetNewSeedID(seedName)

            resultBuilder.AppendFormat("{0:" & StrDup(codeDigitLength, "0") & "}", seedValue)

            Return resultBuilder.ToString

        End Function

        Public Shared Function GetAutoPurchaseFormCode( _
            ByVal preview As Boolean, _
            ByVal departmentID As String) As String

            Dim resultBuilder As New LineStrBuilder

            Dim departmentRow As AssetVisionXAuto.FTs.FT_M_AV_DEPARTMENTRow = _
                AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_DEPARTMENTRow(departmentID)

            If IsNothing(departmentRow) = True Then
                Return String.Empty
            End If

            Dim codePrefix As String = SysInfo.ReadShareSysInfo(Decls.SVN_PURCHASE_CODE_PREFIX)
            Dim codeDigitLength As Integer = CommTK.FInteger(SysInfo.ReadShareSysInfo(Decls.SVN_FORM_CODE_DIGHT_LENGTH))

            resultBuilder.AppendFormat("{0}{1}", codePrefix, departmentRow.DEPARTMENT_CODE)
            If preview = True Then
                resultBuilder.AppendFormat(StrDup(codeDigitLength, "X"))
                Return resultBuilder.ToString
            End If

            Dim seedName As String = String.Format("{0}_{1}", Decls.SPX_PURCHASE_FORM_CODE, resultBuilder.ToString)
            Dim seedValue As Integer = Facade.OpSysConfig.GetNewSeedID(seedName)

            resultBuilder.AppendFormat("{0:" & StrDup(codeDigitLength, "0") & "}", seedValue)

            Return resultBuilder.ToString

        End Function

        Public Shared Function GetAutoRemoveFormCode( _
            ByVal preview As Boolean, _
            ByVal eliminateWay As Integer, _
            ByVal senderID As String) As String

            Dim resultBuilder As New LineStrBuilder

            Dim staffRow As AssetVisionXAuto.FTs.FT_M_AV_STAFFRow = _
                AssetVisionXAuto.Facade.AfBizMaster.GetM_AV_STAFFRow(senderID)

            If IsNothing(staffRow) = True Then
                Return String.Empty
            End If

            Dim codePrefix As String = SysInfo.ReadShareSysInfo(Decls.SVN_REMOVE_CODE_PREFIX)
            Dim codeDigitLength As Integer = CommTK.FInteger(SysInfo.ReadShareSysInfo(Decls.SVN_FORM_CODE_DIGHT_LENGTH))

            resultBuilder.AppendFormat("{0}{1}{2}", codePrefix, eliminateWay, staffRow.STAFF_CODE)
            If preview = True Then
                resultBuilder.AppendFormat(StrDup(codeDigitLength, "X"))
                Return resultBuilder.ToString
            End If

            Dim seedName As String = String.Format("{0}_{1}", Decls.SPX_REMOVE_FORM_CODE, resultBuilder.ToString)
            Dim seedValue As Integer = Facade.OpSysConfig.GetNewSeedID(seedName)

            resultBuilder.AppendFormat("{0:" & StrDup(codeDigitLength, "0") & "}", seedValue)

            Return resultBuilder.ToString

        End Function

        Public Shared Sub FillASADeptPurchaseSituation( _
            ByVal childDepartmentID As String, _
            ByRef refASADeptConfigRowSE As AssetVisionXAuto.FTs.FT_S_AV_ASA_DEPT_CONFIGRowSEntity)

            Dim asaDeptConfigCondition As New AssetVisionXAuto.Facade.AfBizConfig.ConditionOfS_AV_ASA_DEPT_CONFIG(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            asaDeptConfigCondition.Add( _
                AssetVisionXAuto.Facade.AfBizConfig.S_AV_ASA_DEPT_CONFIGColumns.DEPARTMENT_IDColumn, _
                "=", _
                AssetVisionXService.Facade.OpBizMaster.GetASALevelDepartmentID(childDepartmentID, String.Empty))

            asaDeptConfigCondition.Add( _
                AssetVisionXAuto.Facade.AfBizConfig.S_AV_ASA_DEPT_CONFIGColumns.PURCHASE_BUDGETColumn, _
                ">", _
                0)

            AssetVisionXAuto.Facade.AfBizConfig.FillS_AV_ASA_DEPT_CONFIGRowSEntity(refASADeptConfigRowSE, asaDeptConfigCondition)

            If refASADeptConfigRowSE.IsNull = True Then
                Return
            End If

            Dim purchaseTotalList As New AssetVisionXAuto.FTs.FT_MP_AV_SELECT_ASA_DEPT_PERIOD_PURCHASE_AMOUNT_TOTAL
            AssetVisionXService.Facade.OpMP.FillFT_MP_AV_SELECT_ASA_DEPT_PERIOD_PURCHASE_AMOUNT_TOTAL( _
                refASADeptConfigRowSE.DEPARTMENT_ID, _
                refASADeptConfigRowSE.PURCHASE_BUDGET_DATE_FROM, _
                refASADeptConfigRowSE.PURCHASE_BUDGET_DATE_TO, _
                purchaseTotalList)

            refASADeptConfigRowSE.COMPLETED_PURCHASE_AMOUNT = purchaseTotalList(0).TOTAL_PURCHASE_AMOUNT
            refASADeptConfigRowSE.COMPLETED_PURCHASE_QUANTITY = purchaseTotalList(0).TOTAL_PURCHASE_QUANTITY

        End Sub

        Public Shared Function ExecuteRemove(ByVal formID As String, Optional ByVal forced As Boolean = False) As String

            Dim removeRow As AssetVisionXAuto.FTs.FT_H_AV_ASSET_REMOVERow = _
                AssetVisionXAuto.Facade.AfBizManage.GetH_AV_ASSET_REMOVERow(formID)

            If IsNothing(removeRow) = True Then
                Return AssetVisionXService.Decls.MSG_ALERT_00002
            End If


            If forced = True Then
                AssetVisionXAuto.Facade.AfBizManage.ReviseH_AV_ASSET_REMOVERow( _
                    formID, _
                    AfBizManage.H_AV_ASSET_REMOVEColumns.EXECUTOR_CONFIRMEDColumn, _
                    True)
                removeRow.EXECUTOR_CONFIRMED = True
                removeRow.FORM_STATUS = AssetVisionXAuto.Decls.CIVALUE_FORM_STATUS_SUBMIT
            End If

            If removeRow.EXECUTOR_CONFIRMED = False Then
                AssetVisionXAuto.Facade.AfBizManage.ReviseH_AV_ASSET_REMOVERow( _
                    formID, _
                    AfBizManage.H_AV_ASSET_REMOVEColumns.FORM_STATUSColumn, _
                    AssetVisionXAuto.Decls.CIVALUE_FORM_STATUS_SUBMIT)
                Return AssetVisionXService.Decls.MSG_ALERT_00042
            End If


            If removeRow.FORM_STATUS <> AssetVisionXAuto.Decls.CIVALUE_FORM_STATUS_SUBMIT Then
                Return AssetVisionXService.Decls.MSG_ALERT_00033
            End If


            Dim removeDtlList As New AssetVisionXAuto.FTs.FT_H_AV_ASSET_REMOVE_DTL
            Dim removeDtlConditions As New AssetVisionXAuto.Facade.AfBizManage.ConditionOfH_AV_ASSET_REMOVE_DTL(XL.DB.Utils.Condition.LogicOperators.Logic_And)

            removeDtlConditions.Add(AfBizManage.H_AV_ASSET_REMOVE_DTLColumns.FORM_IDColumn, "=", formID)

            Dim assetIDs As ArrayList = _
                AssetVisionXAuto.Facade.AfBizManage.GetH_AV_ASSET_REMOVE_DTL_CVListDistinct(removeDtlConditions, AfBizManage.H_AV_ASSET_REMOVE_DTLColumns.ASSET_IDColumn)

            Dim assetCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfT_AV_ASSET(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            assetCondition.Add(AfBizMaster.T_AV_ASSETColumns.ASSET_IDColumn, True, assetIDs)


            AssetVisionXAuto.Facade.AfBizMaster.ReviseT_AV_ASSETData( _
                assetCondition, _
                AfBizMaster.T_AV_ASSETColumns.ELIMINATE_WAYColumn, _
                removeRow.ELIMINATE_WAY)


            AssetVisionXAuto.Facade.AfBizMaster.ReviseT_AV_ASSETData( _
                assetCondition, _
                AfBizMaster.T_AV_ASSETColumns.CURRENT_LOCATIONColumn, _
                removeRow.LOCATION_ID)

            If removeRow.ELIMINATE_WAY = Decls.DEFAULT_CI_VALUE_ELIMINATE_WAY_MAYBE_LOST Then
                AssetVisionXAuto.Facade.AfBizMaster.ReviseT_AV_ASSETData( _
                    assetCondition, _
                    AfBizMaster.T_AV_ASSETColumns.MAYBE_LOST_DATEColumn, _
                    Facade.OpSysConfig.GetServerTime)

            ElseIf removeRow.ELIMINATE_WAY = Decls.DEFAULT_CI_VALUE_ELIMINATE_WAY_MAYBE_LOST Then
                AssetVisionXAuto.Facade.AfBizMaster.ReviseT_AV_ASSETData( _
                    assetCondition, _
                    AfBizMaster.T_AV_ASSETColumns.MAYBE_LOST_DATEColumn, _
                    CommDecl.NULL_DATE)

            End If


            AssetVisionXAuto.Facade.AfBizManage.ReviseH_AV_ASSET_REMOVERow( _
                formID, _
                AfBizManage.H_AV_ASSET_REMOVEColumns.EXECUTE_DATEColumn, _
                Facade.OpSysConfig.GetServerTime)


            AssetVisionXAuto.Facade.AfBizManage.ReviseH_AV_ASSET_REMOVERow( _
                formID, _
                AfBizManage.H_AV_ASSET_REMOVEColumns.FORM_STATUSColumn, _
                AssetVisionXAuto.Decls.CIVALUE_FORM_STATUS_COMPLETE)

            Dim elimitateWayText = AssetVisionXAuto.Facade.AfCIV.GetCI_ELIMINATE_WAYText( _
                CommDecl.CURRENT_LANGUAGE_OPTION, _
                removeRow.ELIMINATE_WAY, _
                String.Empty)

            Dim assetLifeHistoryList As New AssetVisionXAuto.FTs.FT_H_AV_ASSET_LIFE_LOG
            Dim executorStaffRow As New AssetVisionXAuto.FTs.FT_M_AV_STAFFRowSEntity
            AssetVisionXAuto.Facade.AfBizMaster.FillM_AV_STAFFRowSEntity(executorStaffRow, removeRow.EXECUTOR)

            For Each assetID As String In assetIDs
                assetLifeHistoryList.AddNewH_AV_ASSET_LIFE_LOGRow( _
                    assetID, _
                    CommTK.GetTranslatedString(Decls.TEXT_009, elimitateWayText), _
                    CommTK.GetSyncServerTime, _
                    Guid.NewGuid.ToString, _
                    executorStaffRow.STAFF_CODE, _
                    executorStaffRow.STAFF_NAME)
            Next
            AssetVisionXAuto.Facade.AfBizManage.SaveBatchH_AV_ASSET_LIFE_LOGData(assetLifeHistoryList)


            Return String.Empty

        End Function

        Public Shared Function ExecuteMove(ByVal formID As String, ByVal force As Boolean) As String

            Dim moveRow As AssetVisionXAuto.FTs.FT_H_AV_ASSET_MOVERow = _
                AssetVisionXAuto.Facade.AfBizManage.GetH_AV_ASSET_MOVERow(formID)

            If IsNothing(moveRow) = True Then
                Return AssetVisionXService.Decls.MSG_ALERT_00002
            End If

            Dim executorStaffRow As New AssetVisionXAuto.FTs.FT_M_AV_STAFFRowSEntity
            AssetVisionXAuto.Facade.AfBizMaster.FillM_AV_STAFFRowSEntity(executorStaffRow, moveRow.EXECUTOR)
            If force = True Then
                AssetVisionXAuto.Facade.AfBizManage.ReviseH_AV_ASSET_MOVERow( _
                    formID, _
                    AfBizManage.H_AV_ASSET_MOVEColumns.EXECUTOR_CONFIRMEDColumn, _
                    True)
                moveRow.EXECUTOR_CONFIRMED = True
            End If

            If moveRow.EXECUTOR_CONFIRMED = False Then
                Return AssetVisionXService.Decls.MSG_ALERT_00042
            Else
                AssetVisionXAuto.Facade.AfBizManage.ReviseH_AV_ASSET_MOVERow( _
                    formID, _
                    AfBizManage.H_AV_ASSET_MOVEColumns.FORM_STATUSColumn, _
                    AssetVisionXAuto.Decls.CIVALUE_FORM_STATUS_SUBMIT)
                moveRow.FORM_STATUS = AssetVisionXAuto.Decls.CIVALUE_FORM_STATUS_SUBMIT
            End If

            If moveRow.FORM_STATUS <> AssetVisionXAuto.Decls.CIVALUE_FORM_STATUS_SUBMIT Then
                Return AssetVisionXService.Decls.MSG_ALERT_00033
            End If


            Dim moveDtlList As New AssetVisionXAuto.FTs.FT_H_AV_ASSET_MOVE_DTL
            Dim moveDtlRow As AssetVisionXAuto.FTs.FT_H_AV_ASSET_MOVE_DTLRow
            Dim moveDtlConditions As New AssetVisionXAuto.Facade.AfBizManage.ConditionOfH_AV_ASSET_MOVE_DTL(XL.DB.Utils.Condition.LogicOperators.Logic_And)

            moveDtlConditions.Add(AfBizManage.H_AV_ASSET_MOVE_DTLColumns.FORM_IDColumn, "=", formID)
            AssetVisionXAuto.Facade.AfBizManage.FillFT_H_AV_ASSET_MOVE_DTL( _
                moveDtlConditions, _
                moveDtlList)

            Dim assetIDs As New ArrayList

            Dim alterHistoryList As New AssetVisionXAuto.FTs.FT_H_AV_ASSET_KEEPER_ALTER
            Dim assetLifeHistoryList As New AssetVisionXAuto.FTs.FT_H_AV_ASSET_LIFE_LOG
            Dim assetRow As AssetVisionXAuto.FTs.FT_T_AV_ASSETRow
            Dim currentTime = Facade.OpSysConfig.GetServerTime
            For Each moveDtlRow In moveDtlList
                assetRow = AssetVisionXAuto.Facade.AfBizMaster.GetT_AV_ASSETRow(moveDtlRow.ASSET_ID)

                If IsNothing(assetRow) = True Then
                    Continue For
                End If

                assetIDs.Add(moveDtlRow.ASSET_ID)
                alterHistoryList.AddNewH_AV_ASSET_KEEPER_ALTERRow( _
                    currentTime, _
                    assetRow.CURRENT_LOCATION, _
                    moveRow.EXECUTOR, _
                    moveDtlRow.ASSET_ID, _
                    assetRow.ASSET_KEEPER, _
                    Guid.NewGuid.ToString, _
                    moveDtlRow.DETAIL_ID, _
                    moveRow.TO_KEEPER)


                assetLifeHistoryList.AddNewH_AV_ASSET_LIFE_LOGRow( _
                    assetRow.ASSET_ID, _
                    CommTK.GetTranslatedString(Decls.TEXT_007), _
                    CommTK.GetSyncServerTime, _
                    Guid.NewGuid.ToString, _
                    executorStaffRow.STAFF_CODE, _
                    executorStaffRow.STAFF_NAME)

            Next

            AssetVisionXAuto.Facade.AfBizManage.SaveBatchH_AV_ASSET_LIFE_LOGData(assetLifeHistoryList)

            Dim assetCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfT_AV_ASSET(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            assetCondition.Add(AfBizMaster.T_AV_ASSETColumns.ASSET_IDColumn, True, assetIDs)

            Dim reviseCount = _
                AssetVisionXAuto.Facade.AfBizMaster.ReviseT_AV_ASSETData( _
                    assetCondition, _
                    AfBizMaster.T_AV_ASSETColumns.CURRENT_LOCATIONColumn, _
                    moveRow.TO_LOCATION)

            If reviseCount = 0 Then
                Dim ex As New XLException(XLException.ErrorClassify.ReadDataError)
                ex.SetMessage(Decls.MSG_ALERT_00048)
                CommTK.OutputLog("ExecuteMove failed", String.Format("Asset condition:{0}", assetCondition.ToString))
                Throw ex
            End If
            reviseCount = _
                AssetVisionXAuto.Facade.AfBizMaster.ReviseT_AV_ASSETData( _
                    assetCondition, _
                    AfBizMaster.T_AV_ASSETColumns.CURRENT_DEPARTMENTColumn, _
                    moveRow.TO_DEPARTMENT)

            reviseCount = _
                AssetVisionXAuto.Facade.AfBizMaster.ReviseT_AV_ASSETData( _
                    assetCondition, _
                    AfBizMaster.T_AV_ASSETColumns.ASSET_KEEPERColumn, _
                    moveRow.TO_KEEPER)

            AssetVisionXAuto.Facade.AfBizManage.ReviseH_AV_ASSET_MOVERow( _
                formID, _
                AfBizManage.H_AV_ASSET_MOVEColumns.FORM_STATUSColumn, _
                AssetVisionXAuto.Decls.CIVALUE_FORM_STATUS_COMPLETE)

            AssetVisionXAuto.Facade.AfBizManage.ReviseH_AV_ASSET_MOVERow( _
                formID, _
                AfBizManage.H_AV_ASSET_MOVEColumns.EXECUTE_DATEColumn, _
                CommTK.GetSyncServerTime)

            Facade.OpMP.ExecuteMP_AV_EXECUTE_SYNC_ASSET_RUNNER_BY_LOCATION()

            AssetVisionXAuto.Facade.AfBizManage.ReviseH_AV_ASSET_MOVERow( _
                formID, _
                AfBizManage.H_AV_ASSET_MOVEColumns.FORM_STATUSColumn, _
                AssetVisionXAuto.Decls.CIVALUE_FORM_STATUS_COMPLETE)

            AssetVisionXAuto.Facade.AfBizManage.SaveBatchH_AV_ASSET_KEEPER_ALTERData(alterHistoryList)

            Return String.Empty

        End Function

        Public Shared Function ExecuteAbsent(ByRef absentList As AssetVisionXAuto.FTs.FT_XV_H_AV_ASSET_ABSENT, ByVal forced As Boolean) As String

            'Dim removingAbsentRows
            For Each absentRow As AssetVisionXAuto.FTs.FT_XV_H_AV_ASSET_ABSENTRow In absentList

                Dim assetRow = AssetVisionXAuto.Facade.AfBizMaster.GetT_AV_ASSETRow(absentRow.ASSET_ID)
                If IsNothing(assetRow) = True Then
                    absentRow.ROW_REMARK = Decls.MSG_ALERT_00002
                    Continue For
                End If

                If forced = True Then
                    AssetVisionXAuto.Facade.AfBizManage.ReviseH_AV_ASSET_ABSENTRow( _
                        absentRow.ABSENT_ID, _
                        AfBizManage.H_AV_ASSET_ABSENTColumns.EX_EXECUTOR_CONFIRMEDColumn, _
                        True)
                    absentRow.EX_EXECUTOR_CONFIRMED = True
                End If

                If absentRow.EX_EXECUTOR_CONFIRMED = False Then
                    Continue For
                End If

                Select Case absentRow.ASSET_ABSENT_STATUS
                    Case AssetVisionXAuto.Decls.CIVALUE_ASSET_ABSENT_STATUS_TO_BE_ABSENT

                        If assetRow.ASSET_ABSENT_TYPE <> AssetVisionXAuto.Decls.CIVALUE_ASSET_ABSENT_TYPE_NONE Then
                            absentRow.ROW_REMARK = Decls.MSG_ALERT_00030
                            Continue For
                        End If

                        AssetVisionXAuto.Facade.AfBizManage.ReviseH_AV_ASSET_ABSENTRow( _
                            absentRow.ABSENT_ID, _
                            AfBizManage.H_AV_ASSET_ABSENTColumns.ASSET_ABSENT_STATUSColumn, _
                            AssetVisionXAuto.Decls.CIVALUE_ASSET_ABSENT_STATUS_IS_ABSENT)

                        AssetVisionXAuto.Facade.AfBizMaster.ReviseT_AV_ASSETRow( _
                            absentRow.ASSET_ID, _
                            AfBizMaster.T_AV_ASSETColumns.ASSET_ABSENT_TYPEColumn, _
                            absentRow.ASSET_ABSENT_TYPE)

                    Case AssetVisionXAuto.Decls.CIVALUE_ASSET_ABSENT_STATUS_TO_BE_RETURN

                        If assetRow.ASSET_ABSENT_TYPE = AssetVisionXAuto.Decls.CIVALUE_ASSET_ABSENT_TYPE_NONE Then
                            absentRow.ROW_REMARK = Decls.MSG_ALERT_00031
                            Continue For
                        End If

                        AssetVisionXAuto.Facade.AfBizManage.ReviseH_AV_ASSET_ABSENTRow( _
                            absentRow.ABSENT_ID, _
                            AfBizManage.H_AV_ASSET_ABSENTColumns.ASSET_ABSENT_STATUSColumn, _
                            AssetVisionXAuto.Decls.CIVALUE_ASSET_ABSENT_STATUS_IS_RETURN)

                        AssetVisionXAuto.Facade.AfBizMaster.ReviseT_AV_ASSETRow( _
                            absentRow.ASSET_ID, _
                            AfBizMaster.T_AV_ASSETColumns.ASSET_ABSENT_TYPEColumn, _
                            AssetVisionXAuto.Decls.CIVALUE_ASSET_ABSENT_TYPE_NONE)


                End Select


            Next


            Return String.Empty

        End Function

        Public Shared Function ExecutePurchase(ByVal purchaseID As String) As String

            Dim purchaseRow = AssetVisionXAuto.Facade.AfBizManage.GetT_AV_ASSET_PURCHASERow(purchaseID)

            If IsNothing(purchaseRow) = True Then
                Return AssetVisionXService.Decls.MSG_ALERT_00002
            End If

            If CommTK.FInteger(SysInfo.ReadShareSysInfo(Decls.SVN_ADDRESS_ALONE_DEPT_LEVEL)) = 0 Then
                AssetVisionXAuto.Facade.AfBizManage.ReviseT_AV_ASSET_PURCHASERow( _
                    purchaseID, _
                    AfBizManage.T_AV_ASSET_PURCHASEColumns.EXECUTOR_CONFIRMEDColumn, _
                    True)
                purchaseRow.EXECUTOR_CONFIRMED = True
                purchaseRow.FORM_STATUS = AssetVisionXAuto.Decls.CIVALUE_FORM_STATUS_SUBMIT
            End If

            If purchaseRow.EXECUTOR_CONFIRMED = False Then
                Return AssetVisionXService.Decls.MSG_ALERT_00042
            End If

            If purchaseRow.FORM_STATUS <> AssetVisionXAuto.Decls.CIVALUE_FORM_STATUS_SUBMIT Then
                Return AssetVisionXService.Decls.MSG_ALERT_00033
            End If

            Dim currentASADeptConfigRow As New AssetVisionXAuto.FTs.FT_S_AV_ASA_DEPT_CONFIGRowSEntity
            AssetVisionXService.Facade.OpBizManage.FillASADeptPurchaseSituation( _
                purchaseRow.DEPARTMENT_ID, _
                currentASADeptConfigRow)

            If currentASADeptConfigRow.PURCHASE_BUDGET > 0 AndAlso _
                purchaseRow.PURCHASE_PRICE_TOTAL + currentASADeptConfigRow.COMPLETED_PURCHASE_AMOUNT > currentASADeptConfigRow.PURCHASE_BUDGET Then
                Return AssetVisionXService.Decls.MSG_STATUS_0022
            End If

            Dim assetLifeHistoryList As New AssetVisionXAuto.FTs.FT_H_AV_ASSET_LIFE_LOG
            Dim executorStaffRow As New AssetVisionXAuto.FTs.FT_M_AV_STAFFRowSEntity
            AssetVisionXAuto.Facade.AfBizMaster.FillM_AV_STAFFRowSEntity(executorStaffRow, purchaseRow.EXECUTOR)

            Dim purchaseDtlList As New AssetVisionXAuto.FTs.FT_XV_T_AV_ASSET_PURCHASE_DTL
            Dim purchaseDtlCondition = New AssetVisionXAuto.Facade.AfXV.ConditionOfXV_T_AV_ASSET_PURCHASE_DTL(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            purchaseDtlCondition.Add(AfXV.XV_T_AV_ASSET_PURCHASE_DTLColumns.PURCHASE_IDColumn, "=", purchaseID)

            AssetVisionXAuto.Facade.AfXV.FillFT_XV_T_AV_ASSET_PURCHASE_DTL(purchaseDtlCondition, purchaseDtlList)
            Dim purchaseDtlRow As AssetVisionXAuto.FTs.FT_XV_T_AV_ASSET_PURCHASE_DTLRow
            Dim createAssetList As New AssetVisionXAuto.FTs.FT_T_AV_ASSET
            Dim createAssetRow As AssetVisionXAuto.FTs.FT_T_AV_ASSETRow
            Dim assetBarcode As String

            For Each purchaseDtlRow In purchaseDtlList

                For counter As Integer = 1 To purchaseDtlRow.PURCHASE_QUANTITY

                    assetBarcode = OpBizMaster.GenerateAutoAssetBarcode(False, purchaseDtlRow.CLASSIFY_ID)
                    createAssetRow = createAssetList.AddNewT_AV_ASSETRow( _
                        ACCUMULATED_DEPRECIATION:=0, _
                        ADDITIONAL_COST:=purchaseDtlRow.ADDITIONAL_COST, _
                        ADDITIONAL_COST_REMARKS:=purchaseDtlRow.ADDITIONAL_COST_REMARKS, _
                        ASSET_ABSENT_TYPE:=AssetVisionXAuto.Decls.CIVALUE_ASSET_ABSENT_TYPE_NONE, _
                        ASSET_BARCODE:=assetBarcode, _
                        ASSET_BRAND:=purchaseDtlRow.ASSET_BRAND, _
                        ASSET_COLOR:=purchaseDtlRow.ASSET_COLOR, _
                        ASSET_CONFORMATION:=purchaseDtlRow.ASSET_CONFORMATION, _
                        ASSET_CREATOR:=purchaseDtlRow.CREATOR__STAFF_ID, _
                        ASSET_ID:=Guid.NewGuid.ToString, _
                        ASSET_KEEPER:=purchaseDtlRow.TARGET_ASSET_KEEPER, _
                        ASSET_NAME:=purchaseDtlRow.ASSET_NAME, _
                        ASSET_REMARKS:=purchaseDtlRow.ASSET_REMARKS, _
                        ASSET_USAGE:=purchaseDtlRow.ASSET_USAGE, _
                        BATCH_CODE:=purchaseDtlRow.PURCHASE_CODE, _
                        CHILD_ASSET_COUNT:=0, _
                        CLASSIFY_ID:=purchaseDtlRow.CLASSIFY_ID, _
                        CURRENT_DEPARTMENT:=purchaseDtlRow.TARGET_DEPARTMENT, _
                        CURRENT_LOCATION:=purchaseDtlRow.TARGET_LOCATION, _
                        CURRENT_PRINCIPAL:=purchaseDtlRow.PRINCIPAL, _
                        CURRENT_RUNNER:=purchaseDtlRow.RUNNER, _
                        DEPRECIATION:=purchaseDtlRow.DEPRECIATION, _
                        DEPRECIATION_LIFE:=purchaseDtlRow.DEPRECIATION_LIFE, _
                        ELIMINATE_WAY:=AssetVisionXService.Decls.DEFAULT_CI_VALUE_ELIMINATE_WAY_NONE, _
                        ELIMITATE_AUCTION_PRICE:=0, _
                        GLOBAL_ASSET_CODE:=String.Empty, _
                        GLOBAL_ASSET_REGISTER_STATUS:=AssetVisionXAuto.Decls.CIVALUE_GLOBAL_ASSET_REGISTER_STATUS_NONE, _
                        GUARANTEE_EXPIRE_TRANSACTION:=Decls.DEFAULT_CI_VALUE_GUARANTEE_EXPIRE_TRANSACTION_NONE, _
                        GUARANTEE_FINAL_DATE:=purchaseDtlRow.GUARANTEE_FINAL_DATE, _
                        IMAGE_ID:=Nothing, _
                        IN_ACCOUNT_CREDENCE_CODE:=purchaseDtlRow.PURCHASE_CODE, _
                        IN_ACCOUNT_CREDENCE_TEXT:=Decls.DEFAULT_CI_VALUE_IN_ACCOUNT_CREDENCE_SETTLED, _
                        IN_ACCOUNT_DATE:=CommTK.GetSyncServerTime, _
                        INVENTORY_REQUIRED:=purchaseDtlRow.INVENTORY_REQUIRED, _
                        INVOICE_CODE:=purchaseDtlRow.PURCHASE_CODE, _
                        IS_LEAVE_UNUSED:=False, _
                        LEAVE_UNUSE_ALTER_DATE:=CommTK.GetSyncServerTime, _
                        LEAVE_UNUSE_MARKER:=String.Empty, _
                        MAYBE_LOST_DATE:=CommDecl.NULL_DATE, _
                        MODEL:=purchaseDtlRow.MODEL, _
                        MONTH_DECIPRECIATION:=0, _
                        ORIGION_CODE:=assetBarcode, _
                        ORIGION_VALUE:=purchaseDtlRow.PURCHASE_PRICE + purchaseDtlRow.ADDITIONAL_COST, _
                        OWNER:=purchaseDtlRow.OWNER__OWNER_ID, _
                        PARENT_ASSET:=String.Empty, _
                        PARAL_EXISTS_WITH_CHILDS:=False, _
                        PURCHASE_ID:=purchaseRow.PURCHASE_ID, _
                        PURCHASE_CODE:=purchaseRow.PURCHASE_CODE, _
                        PURCHASE_DTL:=purchaseDtlRow.DETAIL_ID, _
                        PURCHASE_DATE:=CommTK.GetSyncServerTime, _
                        PURCHASE_WAY:=Decls.DEFAULT_CI_VALUE_PURCHASE_WAY_PURCHASE, _
                        SPEC:=purchaseDtlRow.SPEC, _
                        USE_YEARS_LIMIT:=purchaseDtlRow.USE_YEARS_LIMIT, _
                        VENDOR_ID:=purchaseRow.VENDOR_ID, _
                        VENDOR_INFO:=purchaseDtlRow.SUPPLIER_INFO)


                    assetLifeHistoryList.AddNewH_AV_ASSET_LIFE_LOGRow( _
                        createAssetRow.ASSET_ID, _
                        CommTK.GetTranslatedString(Decls.TEXT_010), _
                        CommTK.GetSyncServerTime, _
                        Guid.NewGuid.ToString, _
                        executorStaffRow.STAFF_CODE, _
                        executorStaffRow.STAFF_NAME)

                Next

            Next

            AssetVisionXAuto.Facade.AfBizMaster.SaveBatchT_AV_ASSETData(createAssetList)
            AssetVisionXAuto.Facade.AfBizManage.ReviseT_AV_ASSET_PURCHASERow(purchaseID, AfBizManage.T_AV_ASSET_PURCHASEColumns.FORM_STATUSColumn, AssetVisionXAuto.Decls.CIVALUE_FORM_STATUS_COMPLETE)
            AssetVisionXAuto.Facade.AfBizManage.SaveBatchH_AV_ASSET_LIFE_LOGData(assetLifeHistoryList)

            Return String.Empty
        End Function

        Public Shared Sub FillAssetPropertyItemList(ByVal assetID As String, ByVal classifyID As String, ByVal propertyItemList As AssetVisionXAuto.FTs.FT_T_AV_ASSET_PROPERTY_ITEM)

            Dim assetRow As AssetVisionXAuto.FTs.FT_T_AV_ASSETRow = _
                AssetVisionXAuto.Facade.AfBizMaster.GetT_AV_ASSETRow(assetID)

            If IsNothing(assetRow) = False Then
                classifyID = assetRow.CLASSIFY_ID
            End If

            Dim propertyList As New AssetVisionXAuto.FTs.FT_S_AV_CLASSIFY_PROPERTY
            Dim propertyCondition As New AssetVisionXAuto.Facade.AfBizConfig.ConditionOfS_AV_CLASSIFY_PROPERTY(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            propertyCondition.Add(AfBizConfig.S_AV_CLASSIFY_PROPERTYColumns.CLASSIFY_IDColumn, "=", classifyID)
            AssetVisionXAuto.Facade.AfBizConfig.FillFT_S_AV_CLASSIFY_PROPERTY(propertyCondition, propertyList)

            If propertyList.Count = 0 Then
                Return
            End If

            Dim dbPropertyItemList As New AssetVisionXAuto.FTs.FT_T_AV_ASSET_PROPERTY_ITEM
            Dim propertyItemCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfT_AV_ASSET_PROPERTY_ITEM(XL.DB.Utils.Condition.LogicOperators.Logic_And)

            If IsNothing(assetRow) = False Then

                propertyItemCondition.Add(AfBizMaster.T_AV_ASSET_PROPERTY_ITEMColumns.ASSET_IDColumn, "=", assetID)
                AssetVisionXAuto.Facade.AfBizMaster.FillFT_T_AV_ASSET_PROPERTY_ITEM(propertyItemCondition, dbPropertyItemList)

            End If

            Dim propertyItemRow As AssetVisionXAuto.FTs.FT_T_AV_ASSET_PROPERTY_ITEMRow
            Dim dbPropertyItemRow As AssetVisionXAuto.FTs.FT_T_AV_ASSET_PROPERTY_ITEMRow
            propertyItemList.Clear()

            For Each propertyRow As AssetVisionXAuto.FTs.FT_S_AV_CLASSIFY_PROPERTYRow In propertyList

                propertyItemRow = propertyItemList.NewT_AV_ASSET_PROPERTY_ITEMRow
                propertyItemList.AddT_AV_ASSET_PROPERTY_ITEMRow(propertyItemRow)
                propertyItemRow.ASSET_ID = assetID
                propertyItemRow.PROPERTY_ID = propertyRow.PROPERTY_ID
                propertyItemRow.ROW_REMARK = propertyRow.PROPERTY_NAME

                propertyItemCondition.Clear()
                propertyItemCondition.Add(AfBizMaster.T_AV_ASSET_PROPERTY_ITEMColumns.PROPERTY_IDColumn, "=", propertyRow.PROPERTY_ID)

                dbPropertyItemRow = dbPropertyItemList.FindRowByCondition(propertyItemCondition)
                If IsNothing(dbPropertyItemRow) = False Then
                    propertyItemRow.ITEM_CONTENT = dbPropertyItemRow.ITEM_CONTENT
                End If

            Next

        End Sub

        Public Shared Sub SaveAssetPropertyItemList(ByVal assetID As String, ByVal propertyItemList As AssetVisionXAuto.FTs.FT_T_AV_ASSET_PROPERTY_ITEM)

            Dim assetRow As AssetVisionXAuto.FTs.FT_T_AV_ASSETRow = _
                AssetVisionXAuto.Facade.AfBizMaster.GetT_AV_ASSETRow(assetID)

            If IsNothing(assetRow) = True Then
                Return
            End If


            Dim propertyItemCondition As New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfT_AV_ASSET_PROPERTY_ITEM(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            propertyItemCondition.Add(AfBizMaster.T_AV_ASSET_PROPERTY_ITEMColumns.ASSET_IDColumn, "=", assetID)

            Dim dbPropertyItemList As New AssetVisionXAuto.FTs.FT_T_AV_ASSET_PROPERTY_ITEM
            AssetVisionXAuto.Facade.AfBizMaster.FillFT_T_AV_ASSET_PROPERTY_ITEM(propertyItemCondition, dbPropertyItemList)
            Dim dbPropertyItemRow As AssetVisionXAuto.FTs.FT_T_AV_ASSET_PROPERTY_ITEMRow

            'Dim deleteCondition As New AssetVisionXAuto.Facade.AfBizConfig.ConditionOfT_AV_ASSET_PROPERTY_ITEM(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            'deleteCondition.Add(AfBizConfig.T_AV_ASSET_PROPERTY_ITEMColumns.ASSET_IDColumn, "=", assetID)

            For Each propertyItemRow As AssetVisionXAuto.FTs.FT_T_AV_ASSET_PROPERTY_ITEMRow In propertyItemList

                propertyItemRow.ASSET_ID = assetID
                propertyItemCondition.Clear()
                propertyItemCondition.Add(AfBizMaster.T_AV_ASSET_PROPERTY_ITEMColumns.PROPERTY_IDColumn, "=", propertyItemRow.PROPERTY_ID)
                dbPropertyItemRow = dbPropertyItemList.FindRowByCondition(propertyItemCondition)

                If propertyItemRow.ITEM_CONTENT.Trim.Length > 0 Then
                    If IsNothing(dbPropertyItemRow) = False AndAlso dbPropertyItemRow.ITEM_CONTENT <> propertyItemRow.ITEM_CONTENT.Trim Then
                        dbPropertyItemRow.ITEM_CONTENT = propertyItemRow.ITEM_CONTENT
                    ElseIf IsNothing(dbPropertyItemRow) = True Then
                        dbPropertyItemRow = dbPropertyItemList.NewT_AV_ASSET_PROPERTY_ITEMRow
                        dbPropertyItemList.AddT_AV_ASSET_PROPERTY_ITEMRow(dbPropertyItemRow)
                        dbPropertyItemRow.CloneDataRow(propertyItemRow, False)
                    End If

                ElseIf IsNothing(dbPropertyItemRow) = False Then

                    dbPropertyItemRow.Delete()
                    'deleteCondition.Add(AfBizConfig.T_AV_ASSET_PROPERTY_ITEMColumns.PROPERTY_IDColumn, "=", propertyItemRow.PROPERTY_ID)
                End If
            Next

            AssetVisionXAuto.Facade.AfBizMaster.SaveBatchT_AV_ASSET_PROPERTY_ITEMData(dbPropertyItemList)

        End Sub


        Public Sub FillOverDueAssetAbsentList( _
            ByVal absentType As Integer, _
            ByVal absentList As AssetVisionXAuto.FTs.FT_XV_H_AV_ASSET_ABSENT)

            Dim absentCondition As New AssetVisionXAuto.Facade.AfXV.ConditionOfXV_H_AV_ASSET_ABSENT(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            absentCondition.Add(AfXV.XV_H_AV_ASSET_ABSENTColumns.ASSET_ABSENT_TYPEColumn, "=", absentType)
            absentCondition.Add(AfXV.XV_H_AV_ASSET_ABSENTColumns.RESTORE_DUE_DATEColumn, "<", CommTK.GetSyncServerTime)

            AssetVisionXAuto.Facade.AfXV.FillFT_XV_H_AV_ASSET_ABSENT(absentCondition, absentList)

        End Sub


    End Class

End Namespace
